<?php
/*
 * 此类应用于<head></head>中，主要输出css，js
 * css 位于layout/css
 * js 位于layout/js
 */
class LibsHead {
    private $css = array();
    private $js = array();
    private static $_instance = null;
    private static $_path = '';

    private function __construct() {
        $arr = explode('/index.php', $_SERVER['PHP_SELF']);
        self::$_path = $arr[0].'/layouts';
     }
    /**
     * 单例
     *
     */
    public static function getInstance() {
        if (is_null(self::$_instance)) {
            $cls = __CLASS__;
            self::$_instance = new $cls();
        }
        return self::$_instance;
    }

    /*
     * 增加css
     *
     */
    public function addCss($file){
        $this->css[] = self::$_path.'/css/'.$file;
        return $this;
    }
    
    /*
     * 输出css
     *
     */
    public function getCss(){
        $link = '';
        foreach ($this->css as $c){
            $link .=  "<link type='text/css' rel='stylesheet' href='{$c}'></link>";
        }
        return $link;
    }
    
    public static function css($file) {
        $file = self::$_path.'/css/'.$file;
        $link = "<link type='text/css' rel='stylesheet' href='{$file}'></link>";
        return $link;
    }

    /*
     * 增加js
     *
     */
    public function addScript($file){
        $this->js[] = self::$_path.'/js/'.$file;
        return $this;
    }
    
    /*
     * 输出js
     */
    public function getScript(){
        $js = '';
        foreach($this->js as $j){
            $js .= "<script type='text/javascript' src='{$j}'></script>";
        }
        return $js;
    }
    public static function script($file) {
        $file = self::$_path.'/js/'.$file;
        $link = "<script type='text/javascript' src='{$file}'></script>";
        return $link;
    }
    
    /*
     * 输出title
     */
    public static function setTitle($title){
        echo '<title>'.$title.'</title>';
    } 
}
